{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "# An introduction to solving biological problems with Python\n",
    "\n",
    "- Our course webpage: http://pycam.github.io\n",
    "- Python website: https://www.python.org/ \n",
    "- [Python 3 Standard Library](https://docs.python.org/3/library/index.html])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Learning objectives\n",
    "\n",
    "- **Recall** what we've learned so far on variables, common data types, conditions and loops\n",
    "- **Propose and create** solutions using these concepts in an exercise\n",
    "- **Employ** functions and modules to reuse code\n",
    "- **Practice** reading and writing files with Python\n",
    "- **Use** third-party library BioPython\n",
    "- **Solve** more complex exercises"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Course schedule - day two\n",
    "\n",
    "- 09:30-10:30: [1h00] **Introduction** - What we've learned so far...\n",
    "- 10:30-10:45: *break*\n",
    "- 10:45-11:45: [1h00] **Session 2.1** - Functions\n",
    "- 11:45-12:00: *break*\n",
    "- 12:00-13:00: [1h00] **Session 2.2** - Exercices and Modules\n",
    "- 13:00-14:00: *lunch break*\n",
    "- 14:00-15:30: [1h30] **Session 2.3** - Files\n",
    "- 15:30-15:45: *break*\n",
    "- 15:45-16:45: [1h00] **Session 2.4** - BioPython"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## What we've learned so far\n",
    "\n",
    "- Simple data types, Collections\n",
    "- Conditional execution\n",
    "- Loops\n",
    "- Functions used so far..."
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Collections"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {
    "collapsed": false,
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A tuple: (2, 3, 4, 5)\n",
      "First element of tuple: 2\n"
     ]
    }
   ],
   "source": [
    "## Tuple - immutable\n",
    "example_tuple = (2, 3, 4, 5)\n",
    "print('A tuple:', example_tuple)\n",
    "print('First element of tuple:', example_tuple[0])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {
    "collapsed": false,
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A list: [2, 3, 4, 5]\n",
      "First element of list: 2\n",
      "Appended list: [2, 3, 4, 5, 12]\n",
      "Modified list: [45, 3, 4, 5, 12]\n"
     ]
    }
   ],
   "source": [
    "## List\n",
    "example_list = [2, 3, 4, 5]\n",
    "print('A list:', example_list)\n",
    "print('First element of list:', example_list[0])\n",
    "example_list.append(12)\n",
    "print('Appended list:', example_list)\n",
    "example_list[0] = 45\n",
    "print('Modified list:', example_list)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 45,
   "metadata": {
    "collapsed": false,
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Here is a string: ATGTCATTT\n",
      "First character: A\n",
      "Number of characters in text 9\n"
     ]
    }
   ],
   "source": [
    "## String - immutable, tuple of characters\n",
    "text = \"ATGTCATTT\"\n",
    "print('Here is a string:', text)\n",
    "print('First character:', text[0])\n",
    "print('Number of characters in text', len(text))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {
    "collapsed": false,
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A set: {1, 2, 4, 5, 6}\n"
     ]
    }
   ],
   "source": [
    "## Set - unique unordered elements\n",
    "example_set = set([1,2,2,2,2,4,5,6,6,6])\n",
    "print('A set:', example_set)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {
    "collapsed": false,
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A dictionary: {'G': 'Guanine', 'T': 'Thymine', 'A': 'Adenine', 'C': 'Cytosine'}\n",
      "Value associated to key C: Cytosine\n"
     ]
    }
   ],
   "source": [
    "## Dictionary\n",
    "example_dictionary = {\"A\": \"Adenine\", \n",
    "                      \"C\": \"Cytosine\", \n",
    "                      \"G\": \"Guanine\", \n",
    "                      \"T\": \"Thymine\"}\n",
    "print('A dictionary:', example_dictionary)\n",
    "print('Value associated to key C:', example_dictionary['C'])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Conditional execution"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 48,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Is 2 < 5? True\n",
      "Is 2 == 5? False\n",
      "Is 2 < 5 and 2 > 1? True\n",
      "x is equal to 2\n"
     ]
    }
   ],
   "source": [
    "x = 2\n",
    "print('Is 2 < 5?', x < 5)\n",
    "print('Is 2 == 5?', x == 5)\n",
    "print('Is 2 < 5 and 2 > 1?', (x < 5) & (x > 1))\n",
    "\n",
    "if x == 2:\n",
    "    print('x is equal to 2')"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Loops"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 49,
   "metadata": {
    "collapsed": false,
    "slideshow": {
     "slide_type": "-"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The element in list is: A\n",
      "The element in list is: C\n",
      "The element in list is: A\n",
      "The element in list is: T\n",
      "The element in list is: G\n"
     ]
    }
   ],
   "source": [
    "example_list = ['A', 'C', 'A', 'T', 'G']\n",
    "\n",
    "## Looping through a list\n",
    "for element in example_list:\n",
    "    print(\"The element in list is:\", element)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {
    "collapsed": false,
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "The index in loop is 0 and its corresponding element is in the list A\n",
      "The index in loop is 1 and its corresponding element is in the list C\n",
      "The index in loop is 2 and its corresponding element is in the list A\n",
      "The index in loop is 3 and its corresponding element is in the list T\n",
      "The index in loop is 4 and its corresponding element is in the list G\n"
     ]
    }
   ],
   "source": [
    "example_list = ['A', 'C', 'A', 'T', 'G']\n",
    "\n",
    "## Looping through list of indices using range() method\n",
    "for index in range(len(example_list)):\n",
    "    print(\"The index in loop is\", index, 'and its corresponding element is in the list', example_list[index])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 51,
   "metadata": {
    "collapsed": false,
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A found, the corresponding value is Adenine for item at position 0\n",
      "Other value for item at position 1\n",
      "A found, the corresponding value is Adenine for item at position 2\n",
      "T found, the corresponding value is Thymine for item at position 3\n",
      "Other value for item at position 4\n"
     ]
    }
   ],
   "source": [
    "## Looping using enumerate()\n",
    "example_list = ['A', 'C', 'A', 'T', 'G']\n",
    "example_dictionary = {\"A\": \"Adenine\", \"C\": \"Cytosine\", \"G\": \"Guanine\", \"T\": \"Thymine\"}\n",
    "\n",
    "for index, value in enumerate(example_list):\n",
    "    if value == 'A':\n",
    "        print(value, \"found, the corresponding value is\", example_dictionary[value], 'for item at position', index)\n",
    "    elif value == 'T':\n",
    "        print(value, \"found, the corresponding value is\", example_dictionary[value], 'for item at position', index)\n",
    "    else:\n",
    "        print(\"Other value\", 'for item at position', index)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Functions used so far..."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 52,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "There are 5 elements in the list ['A', 'C', 'A', 'T', 'G']\n",
      "['ATG', 'TCA', 'CCG', 'GGC']\n"
     ]
    }
   ],
   "source": [
    "example_list = ['A', 'C', 'A', 'T', 'G']\n",
    "print('There are', len(example_list), 'elements in the list', example_list)\n",
    "print(\"ATG TCA CCG GGC\".split())"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Exercise 2.0.1\n",
    "\n",
    "- Create a string variable with the lyrics of Imagine by John Lennon, 1971. Split into words. Print the total number of words, and the number of unique words. Calculate the frequency of each word and store the result into a dictionary. Print each unique word along with its frequency. Find the most frequent word in the song, print it with its frequency.\n",
    "\n",
    "<center><img src=\"https://upload.wikimedia.org/wikipedia/en/1/1d/John_Lennon_-_Imagine_John_Lennon.jpg\"/></center>"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {
    "collapsed": false,
    "slideshow": {
     "slide_type": "subslide"
    }
   },
   "outputs": [],
   "source": [
    "lyrics = \"\"\"\n",
    "Imagine there's no Heaven\n",
    "It's easy if you try\n",
    "No Hell below us\n",
    "Above us only sky\n",
    "\n",
    "Imagine all the people\n",
    "Living for today\n",
    "Aaa haa\n",
    "\n",
    "Imagine there's no countries\n",
    "It isn't hard to do\n",
    "Nothing to kill or die for\n",
    "And no religion too\n",
    "\n",
    "Imagine all the people\n",
    "Living life in peace\n",
    "Yoo hoo\n",
    "\n",
    "You may say I'm a dreamer\n",
    "But I'm not the only one\n",
    "I hope someday you'll join us\n",
    "And the world will be as one\n",
    "\n",
    "Imagine no possessions\n",
    "I wonder if you can\n",
    "No need for greed or hunger\n",
    "A brotherhood of man\n",
    "\n",
    "Imagine all the people\n",
    "Sharing all the world\n",
    "Yoo hoo\n",
    "\n",
    "You may say I'm a dreamer\n",
    "But I'm not the only one\n",
    "I hope someday you'll join us\n",
    "And the world will live as one\n",
    "\"\"\""
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "slideshow": {
     "slide_type": "slide"
    }
   },
   "source": [
    "## Next session\n",
    "\n",
    "Go to our next notebook: [Introduction_to_python_day_2_session_1](Introduction_to_python_day_2_session_1.ipynb)"
   ]
  }
 ],
 "metadata": {
  "celltoolbar": "Slideshow",
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 0
}
